/**
 * Copyright 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#if DEBUG_VERSION
if (!dafos) {
  throw new Error("Dafos namespace not found!");
}
#endif

dafos.AppEngine = function() {
}

dafos.AppEngine.prototype.saveSample = function(name, text, viewername, callback) { 
  var params = {};
  var postdata = {
    "dafos_sample_name" : name,
    "dafos_sample_text" : text,
    "viewer_name" : viewername
  };

  params[gadgets.io.RequestParameters.AUTHORIZATION] = 
    gadgets.io.AuthorizationType.SIGNED;
  params[gadgets.io.RequestParameters.CONTENT_TYPE] = 
    gadgets.io.ContentType.JSON;
  params[gadgets.io.RequestParameters.METHOD] = 
    gadgets.io.MethodType.POST;
  params[gadgets.io.RequestParameters.POST_DATA] = 
    gadgets.io.encodeValues(postdata);

  var url = "@server.deploy.base@/gadgetapi/savesample";
  gadgets.io.makeRequest(url, dafos.bind(this.saveResponse, this, callback), params);
};

dafos.AppEngine.prototype.deleteRecord = function(id, callback) { 
  var params = {};
  var postdata = {};
  
  params[gadgets.io.RequestParameters.AUTHORIZATION] = 
    gadgets.io.AuthorizationType.SIGNED;
  params[gadgets.io.RequestParameters.CONTENT_TYPE] = 
    gadgets.io.ContentType.JSON;
  params[gadgets.io.RequestParameters.METHOD] = 
    gadgets.io.MethodType.POST;
  params[gadgets.io.RequestParameters.POST_DATA] = 
    gadgets.io.encodeValues(postdata);

  var url = "@server.deploy.base@/gadgetapi/deleterecord/" + id;
  gadgets.io.makeRequest(url, dafos.bind(this.saveResponse, this, callback), params);
};

dafos.AppEngine.prototype.canSave = function(callback) { 
  var params = {};
  
  params[gadgets.io.RequestParameters.AUTHORIZATION] = 
    gadgets.io.AuthorizationType.SIGNED;
  params[gadgets.io.RequestParameters.CONTENT_TYPE] = 
    gadgets.io.ContentType.JSON;

  //hi5 has a bug on signed GETs
  params[gadgets.io.RequestParameters.METHOD] = 
    gadgets.io.MethodType.POST;
  params[gadgets.io.RequestParameters.POST_DATA] = 
    gadgets.io.encodeValues({"test" : "test"});

  var url = "@server.deploy.base@/gadgetapi/cansave";
  gadgets.io.makeRequest(url, dafos.bind(this.saveResponse, this, callback), params);
};


dafos.AppEngine.prototype.getOwnerSamples = function(callback) { 
  var params = {};
  
  params[gadgets.io.RequestParameters.AUTHORIZATION] = 
    gadgets.io.AuthorizationType.SIGNED;
  params[gadgets.io.RequestParameters.CONTENT_TYPE] = 
    gadgets.io.ContentType.JSON;
    
  //hi5 has a bug on signed GETs
  params[gadgets.io.RequestParameters.METHOD] = 
    gadgets.io.MethodType.POST;
  params[gadgets.io.RequestParameters.POST_DATA] = 
    gadgets.io.encodeValues({"test" : "test"});


  var url = "@server.deploy.base@/gadgetapi/samples";
  gadgets.io.makeRequest(url, dafos.bind(this.saveResponse, this, callback), params);
};

dafos.AppEngine.prototype.getSample = function(id, callback) { 
  var params = {};
  
  params[gadgets.io.RequestParameters.AUTHORIZATION] = 
    gadgets.io.AuthorizationType.SIGNED;
  params[gadgets.io.RequestParameters.CONTENT_TYPE] = 
    gadgets.io.ContentType.JSON;
    
  //hi5 has a bug on signed GETs
  params[gadgets.io.RequestParameters.METHOD] = 
    gadgets.io.MethodType.POST;
  params[gadgets.io.RequestParameters.POST_DATA] = 
    gadgets.io.encodeValues({"test" : "test"});


  var url = "@server.deploy.base@/gadgetapi/sample/" + id;
  gadgets.io.makeRequest(url, dafos.bind(this.saveResponse, this, callback), params);
};


#if EXPORTTAB
dafos.AppEngine.prototype.saveXmlSpec = function(name, text, viewername, callback) { 
  var params = {};
  var postdata = {
    "dafos_xmlspec_name" : name,
    "dafos_xmlspec_text" : text,
    "viewer_name" : viewername
  };

  params[gadgets.io.RequestParameters.AUTHORIZATION] = 
    gadgets.io.AuthorizationType.SIGNED;
  params[gadgets.io.RequestParameters.CONTENT_TYPE] = 
    gadgets.io.ContentType.JSON;
  params[gadgets.io.RequestParameters.METHOD] = 
    gadgets.io.MethodType.POST;
  params[gadgets.io.RequestParameters.POST_DATA] = 
    gadgets.io.encodeValues(postdata);

  var url = "@server.deploy.base@/gadgetapi/savexmlspec";
  gadgets.io.makeRequest(url, dafos.bind(this.saveResponse, this, callback), params);
};

dafos.AppEngine.prototype.getOwnerXmlSpecs = function(callback) { 
  var params = {};
  
  params[gadgets.io.RequestParameters.AUTHORIZATION] = 
    gadgets.io.AuthorizationType.SIGNED;
  params[gadgets.io.RequestParameters.CONTENT_TYPE] = 
    gadgets.io.ContentType.JSON;
    
  //hi5 has a bug on signed GETs
  params[gadgets.io.RequestParameters.METHOD] = 
    gadgets.io.MethodType.POST;
  params[gadgets.io.RequestParameters.POST_DATA] = 
    gadgets.io.encodeValues({"test" : "test"});

  var url = "@server.deploy.base@/gadgetapi/xmlspecs";
  gadgets.io.makeRequest(url, dafos.bind(this.saveResponse, this, callback), params);
};

dafos.AppEngine.prototype.getXmlTemplate = function(callback) {
  var params = {};
  
  params[gadgets.io.RequestParameters.CONTENT_TYPE] = 
    gadgets.io.ContentType.TEXT;

  var url = "@server.deploy.base@/static/xmlspec-template.xml";
  gadgets.io.makeRequest(url, dafos.bind(this.saveResponse, this, callback), params);
};

dafos.AppEngine.prototype.getXmlSpec = function(id, callback) { 
  var params = {};
  
  params[gadgets.io.RequestParameters.AUTHORIZATION] = 
    gadgets.io.AuthorizationType.SIGNED;
  params[gadgets.io.RequestParameters.CONTENT_TYPE] = 
    gadgets.io.ContentType.JSON;
    
  //hi5 has a bug on signed GETs
  params[gadgets.io.RequestParameters.METHOD] = 
    gadgets.io.MethodType.POST;
  params[gadgets.io.RequestParameters.POST_DATA] = 
    gadgets.io.encodeValues({"test" : "test"});


  var url = "@server.deploy.base@/gadgetapi/xmlspec/" + id;
  gadgets.io.makeRequest(url, dafos.bind(this.saveResponse, this, callback), params);
};
#endif



#if ORKUT
dafos.AppEngine.prototype.clearCache = function(url, callback) { 
  var params = {};
  
  var postdata = {
    "dafos_clear_cache_url" : url
  };
  
  params[gadgets.io.RequestParameters.AUTHORIZATION] = 
    gadgets.io.AuthorizationType.SIGNED;
  params[gadgets.io.RequestParameters.METHOD] = 
    gadgets.io.MethodType.POST;
  params[gadgets.io.RequestParameters.CONTENT_TYPE] = 
    gadgets.io.ContentType.JSON;
  params[gadgets.io.RequestParameters.POST_DATA] = 
    gadgets.io.encodeValues(postdata);

  var url = "@server.deploy.base@/gadgetapi/clearcache";
  gadgets.io.makeRequest(url, dafos.bind(this.saveResponse, this, callback), params);
};
#endif

#if SNIPPETSTAB
dafos.AppEngine.prototype.getGlobalSnippets = function(callback) {
  var params = {};
  
  params[gadgets.io.RequestParameters.CONTENT_TYPE] = 
    gadgets.io.ContentType.JSON;

  var url = "@server.deploy.base@/gadgetapi/globalsamples";
  gadgets.io.makeRequest(url, dafos.bind(this.saveResponse, this, callback), params);
}
#endif

dafos.AppEngine.prototype.saveResponse = function(callback, data) {
  if (typeof callback == "function") {
    if (data.data) {
      callback(data.data);
    } else {
      LOG("[AppEngine.saveResponse] error:", data);
      if (data.text) {
        dafos.ui.setError("Server issue: " + data.text);
      }
      if (!data.errors) {
        data.errors = [ 'Unknown' ];
      }
      callback({"error": data.errors[0] });
    }
  }
};
